<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="pyjm" label="" class="w-80">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/卡号/手机号/身份证号" />
        </a-form-item>
        <a-form-item name="dateRange" label="挂号日期" class="w-80 mx-5">
          <a-range-picker v-model:value="formState['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="getTollCollectorData" />
        </a-form-item>
        <a-form-item label="就诊事项" class="!w-64 ml-2">
          <a-select v-model:value="formState.dictOutpMatterId" placeholder="请选择" allow-clear>
            <a-select-option v-for="(item, index) in dictOutList" :key="index" :value="item.dictOutpMatterId">{{ item.matterName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态" class="!w-64">
          <a-select v-model:value="formState.sszt" placeholder="请选择" allow-clear>
            <a-select-option :value="0">未手术</a-select-option>
            <a-select-option :value="1">已手术</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable fill :loading="loading" column-code="000790" :scroll="{ x: 3200 }" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex == 'ghztName'">
            <a-tag v-if="record.ghzt === 0" color="blue">候诊中</a-tag>
            <a-tag v-else-if="record.ghzt === 1" color="green">已就诊</a-tag>
            <a-tag v-else-if="record.ghzt === 2" color="red">已退号</a-tag>
            <a-tag v-else-if="record.ghzt === 5" color="orange">未签到</a-tag>
            <a-tag v-else color="green">已完诊</a-tag>
          </template>
          <template v-if="column.dataIndex == 'registerPath'">
            <div>{{ ['窗口', '掌医', '自助机'][record.registerPath] || '-' }}</div>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 跟进 </a-button>
            <a-button type="link" size="small" class="ml-2" @click="onOperation(record)"> {{ record.aptmLsh ? '修改预约' : '手术预约' }} </a-button>
            <BaseButton confirm-text="确认作废？" type="link" size="small" danger @click="onCancel(record)">取消手术</BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
    <OperationEdit v-model:open="operationOpen" :row-data="operationData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import OperationEdit from '@/views/operation-eyes/operation-registration/Edit.vue'
import useUserStore from '@/store/user'
import Edit from './Edit.vue'

const { empId } = useUserStore()

const dictOutList = ref([])
const rowData = ref({})
const operationData = ref({})
const editOpen = ref(false)
const operationOpen = ref(false)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().subtract(1, 'month').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  brZxs: empId
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzdj/getOutpRecordInfoList',
  params: formState.value
})

const onHandleEdit = (row) => {
  rowData.value = { ...row }
  editOpen.value = true
}
const onOperation = async (row) => {
  if (row.aptmLsh) {
    const res = await post('/operation/opex/chekDoApp', { brId: row.brId })
    if (res.code !== 0) return message.error(res.msg)
    if (res.data) {
      message.error(res.data)
    } else {
      const { data, code, msg } = await post('/operation/opex/getAppointmentRecord', { aptmLsh: row.aptmLsh })
      if (code !== 0) return message.error(msg)
      operationData.value = data
      operationOpen.value = true
    }
  } else {
    operationData.value = { ...row }
    operationOpen.value = true
  }
}

// 获取就诊事项
const getDictOutList = async () => {
  const res = await post('/outp/mzdj/getDictOutpMatterNumberList')
  if (res.code !== 0) return message.error(res.msg)
  dictOutList.value = res.data
  const selectDefault = dictOutList.value.find((i) => i.matterName === '手术初诊')
  selectDefault && (formState.value.dictOutpMatterId = selectDefault?.dictOutpMatterId)
}
const onCancel = async (row) => {
  const res = await post('/outp/mzdj/cancelOphthOprAppoitment', { brId: row.brId })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg || '操作成功')
  getData()
}
const onSuccess = () => {
  editOpen.value = false
  operationOpen.value = false
  getData()
}
onMounted(async () => {
  await getDictOutList()
  getData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
